<?php
	session_start();
	require("../inc/functions.php");
	if(!isEmployee())
		header("Location: ../index.php");
	require_once("../inc/models/products_model.php");
	if(isset($_POST['submit'])){
		$row = $_POST;
		
		if(is_uploaded_file($_FILES['image']['tmp_name'])){
			$target_path = "../inc/images/";
			$target_path = $target_path.basename($_FILES['image']['name']);
			$type = pathinfo($_FILES['image']['tmp_name']);
			//$type = strtolower(file_extension($_FILES['image']['name']));
			
			/*if ($type != "jpg" && $type != "bmp" &&  $type != "gif" && $type != "png" && $type !="jpeg")
    		{
            	$error[] = "Not a valid file type: $type";   
    		}*/
    		if(!move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) 
   			 {
   			 	die("image did not upload properly");
       			 /*$error[] = "There was an error uploading the file, please try again!";
				 echo mysql_error();*/
   			 }
   			 
			 $image = trim($_FILES['image']['name']);
		}
		else {
				$image =NULL;
		}
		
		if(is_uploaded_file($_FILES['thumbnail']['tmp_name'])){
			$target_path = "../inc/images/thumbnails/";
			$target_path = $target_path.basename($_FILES['thumbnail']['name']);
			$type = pathinfo($_FILES['thumbnail']['tmp_name']);
			//$type = strtolower(file_extension($_FILES['image']['name']));
			
			/*if ($type != "jpg" && $type != "bmp" &&  $type != "gif" && $type != "png" && $type !="jpeg")
    		{
            	$error[] = "Not a valid file type: $type";   
    		}*/
    		if(!move_uploaded_file($_FILES['thumbnail']['tmp_name'], $target_path)) 
   			 {
   			 	die("Thumbnail did not upload properly");
       			 /*$error[] = "There was an error uploading the file, please try again!";
				 echo mysql_error();*/
   			 }
   			 
			 $thumbnail = trim($_FILES['thumbnail']['name']);
		}
		else {
				$thumbnail = NULL;
		}
		
		EditProduct($row,$image,$thumbnail);
		header("Location: index.php");
	}
	else{
		$results = GetProduct($_REQUEST['id']);
		$companies = GetCompanies();
		$product_category = GetProductCategories();
	}
?>

<!DOCTYPE html>
<html lang="en">
	<?php include('../inc/head.php'); ?>
	<body>
	<?php include('../inc/navigation.php'); ?>
			<div class="span9">
				<div class="row-fluid">
					<h2><?=$results['name'] ?></h2>
					
					<form class="well" enctype="multipart/form-data" method="post" action="#">
						<div class="control-group">
			            	<label class="control-label" for="companies_id">Company:</label>
			             <div class="controls">
			             		<select name="companies_id" id="companies_id">
			             			<?php while($rs = $companies->fetch_assoc()): ?>
			             			<option value="<?=$rs['id'] ?>"><?=$rs['name']?></option>
			             			<?php endwhile; ?>
			             		</select>
			               </div>
			            </div>
			            <div class="control-group">
			            	<label class="control-label" for="product_category_id">Product Category:</label>
			             <div class="controls">
			             		<select name="product_category_id" id="product_category_id">
			             			<?php while($rs = $product_category->fetch_assoc()): ?>
			             			<option value="<?=$rs['id'] ?>"><?=$rs['name']?></option>
			             			<?php endwhile; ?>
			             		</select>
			               </div>
			            </div>
			            <div class="control-group">
			            	<label class="control-label" for="sku">SKU:</label>
			             <div class="controls">
			                <input type="text" name="sku" id="sku" value="<?=$results['sku']?>"/>
			               </div>
			            </div>
						<div class="control-group">
			            	<label class="control-label" for="name">Product Name:</label>
			             <div class="controls">
			                <input type="text" name="name" id="name" value="<?=$results['name']?>"/>
			               </div>
			            </div>
			            <div class="control-group">
			            	<label class="control-label" for="price">Price:</label>
			             <div class="controls">
			                <input type="text" name="price" id="price" value="<?=$results['price']?>"/>
			               </div>
			            </div>
			            <div class="control-group">
			            	<label class="control-label" for="weight">Weight(In Pounds):</label>
			             <div class="controls">
			                <input type="text" name="weight" id="weight" value="<?=$results['weight']?>"/>
			               </div>
			            </div>
			            <div class="control-group">
			            	<label class="control-label" for="description">Description:</label>
			             <div class="controls">
			                <textarea name="description" id="description" style="width: 100%; resize: none;"><?=$results['description']?></textarea>
			               </div>
			            </div>
			            <div class="control-group">
			            	<label class="control-label" for="image">Image:</label>
			             <div class="controls">
			                <input type="file" name="image" id="image"/>
			               </div>
			            </div>
			            <div class="control-group">
			            	<label class="control-label" for="thumbnail">Thumbnail:</label>
			             <div class="controls">
			                <input type="file" name="thumbnail" id="thumbnail" value="<?=$results['thumbnail']?>"/>
			               </div>
			            </div>
			            <input type="hidden" name="id" id="id" value="<?=$results['id']?>"/>
						<input type="submit" name="submit" value="Submit" />
					</form>
				</div>
			</div>
		</div>
	</div>
	
	<!-- All Bootstrap JavaScript Files -->
	<script src="../inc/bootstrap/js/jquery.js"></script>
    <script src="../inc/bootstrap/js/bootstrap.js"></script>
	
	</body>
</html>
